import Sequelize from 'sequelize'
import sequelize from './../../../database'

export const CustomerGroup = sequelize.define('customer_group',{
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
    },
    customer_group_name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    customer_group_type: {
        type: Sequelize.INTEGER,
        allowNull: false,
    },
    status: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false
    },
    created_man: {
        type: Sequelize.STRING,
        allowNull: false
    },
    memo: {
        type: Sequelize.STRING
    }
},{
    freezeTableName:false,
    tableName: 'customer_group',
    updatedAt:'updated_at',
    createdAt:'created_at'
})

class CustomerGroupModel{
    insert(record){
        return CustomerGroup.create(record);
    }
    update(record){
        return CustomerGroup.update(record,
            {
                'where':{
                    'id': record.id
                },
                fields: ['memo','customer_group_type', 'customer_group_name', 'status']
            }
        )
    }
    delete(id){
        return CustomerGroup.destroy({
            where: {
                id: id
            }
        });
    }
    findById(id){
        return CustomerGroup.findById(id, {
            'attributes': ['id', 'customer_group_type', 'customer_group_name', 'status', 'memo']
        });
    }
    findAll(){
        return CustomerGroup.findAll({
            'attributes': ['id', 'customer_group_type', 'customer_group_name', 'status', 'memo']
        })
    }
    findByPage(record){
        return CustomerGroup.findAndCount({
            'attributes': ['id', 'customer_group_type', 'customer_group_name', 'status', 'memo'],
            limit:record.page_size,
            offset: record.page_size * (record.page_no -1)
        })
    }
}
module.exports = new CustomerGroupModel();